package org.csy.work.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.csy.work.dao.UsersDao;
import org.csy.work.dao.impl.UsersDaoImpl;
import org.csy.work.entity.Users;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author CSY
 * @date 2024-11-26
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String userName = req.getParameter("userName");
        String pwd = req.getParameter("pwd");
        UsersDao usersDao = new UsersDaoImpl();
        Users user = usersDao.login(userName);
        String message = "";
        if(user != null){
            if(user.getPassword().trim().equals(pwd.trim())){
                message = "Yes";
            }else{
                message = "密码错误！";
            }
        }else{
            message = "账号不存在！";
        }
        PrintWriter out = resp.getWriter();

        out.println(message);

        out.flush();
        out.close();
    }
}
